package semaphore;

import java.sql.Connection;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class MainDemo {

    private static  Random random=new Random();

    private static SemaphoreDBPool semaphoreDBPool=new SemaphoreDBPool();

    public static void main(String[] args) {


        for (int i=0;i<50;i++){
            new MyThread().start();
        }
    }

    public static class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            Long stat=System.currentTimeMillis();
            try{
                Connection connection=semaphoreDBPool.getConnection();
                System.out.println(Thread.currentThread().getId()+"线程获取连接共花费"+(System.currentTimeMillis()-stat)+"ms");
                TimeUnit.MILLISECONDS.sleep(100+random.nextInt(100));
                semaphoreDBPool.returnConnection(connection);
            }catch (Exception e){

            }
        }
    }
}
